﻿using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.ComponentModel;
namespace WPFExcelReport
{
 
     public partial class UILMFKART : ViewBase
    {
        #region ==CARDNO===========
        public static readonly DependencyProperty CARDNOProperty = DependencyProperty.Register("CARDNO",
            typeof(string), typeof(UILMFKART),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnCARDNOChanged));
        public string CARDNO
        {
            get
            {
                return (string)GetValue(CARDNOProperty);
            }
            set
            {
                SetValue(CARDNOProperty, value); 
            }
        }
        private static void OnCARDNOChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UILMFKART;
            if (src != null)
            {
                src.CARDNO = (string)e.NewValue;
                src.OnCARDNoChg((string)e.NewValue);
            }
        }
        #endregion

        #region ==CGRP===========
        public static readonly DependencyProperty CGRPProperty = DependencyProperty.Register("CGRP",
            typeof(string), typeof(UILMFKART),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnCGRPChanged));
        public string CGRP
        {
            get
            {
                return (string)GetValue(CGRPProperty);
            }
            set
            {
                SetValue(CGRPProperty, value);
                ((VLMFKART)MV).CGRP = value;
            }
        }
        private static void OnCGRPChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UILMFKART;
            if (src != null)
            {
                src.CGRP = (string)e.NewValue;
            }
        }
        #endregion
        #region ==CRGB===========
        public static readonly DependencyProperty CRGBProperty = DependencyProperty.Register("CRGB",
            typeof(string), typeof(UILMFKART),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnCRGBChanged));
        public string CRGB
        {
            get
            {
                return (string)GetValue(CRGBProperty);
            }
            set
            {
                SetValue(CRGBProperty, value); 
            }
        }
        private static void OnCRGBChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UILMFKART;
            if (src != null)
            {
                src.CRGB = (string)e.NewValue;
            }
        }
        #endregion


        #region ==CR===========
        public static readonly DependencyProperty CRProperty = DependencyProperty.Register("CR",
            typeof(string), typeof(UILMFKART),
            new FrameworkPropertyMetadata("000", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnCRChanged));
        public string CR
        {
            get
            {
                return (string)GetValue(CRProperty);
            }
            set
            {
                SetValue(CRProperty, value);
            }
        }
        private static void OnCRChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var source = sender as UILMFKART;
            if (source != null)
            {
                source.CR = Helpers.Helper.Right("000" + (string)e.NewValue, 3);
                source.TMV.LMFRGBWERT1 = source.CR; 
                source.CRGB = source.CR + source.CG + source.CB;
            }
        }
        #endregion
  

        #region ==CG===========
        public static readonly DependencyProperty CGProperty = DependencyProperty.Register("CG",
            typeof(string), typeof(UILMFKART),
            new FrameworkPropertyMetadata("000", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnCGChanged));
        public string CG
        {
            get
            {
                return (string)GetValue(CGProperty);
            }
            set
            {
                SetValue(CGProperty, value);
            }
        }
        private static void OnCGChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var source = sender as UILMFKART;
            if (source != null)
            {
                source.CG = Helpers.Helper.Right("000" + (string)e.NewValue, 3);
                source.TMV.LMFRGBWERT2= source.CG;
                source.CRGB = source.CR + source.CG + source.CB;
            }
        }
        #endregion

        #region ==CB===========
        public static readonly DependencyProperty CBProperty = DependencyProperty.Register("CB",
            typeof(string), typeof(UILMFKART),
            new FrameworkPropertyMetadata("000", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnCBChanged));
        public string CB
        {
            get
            {
                return (string)GetValue(CBProperty);
            }
            set
            {
                SetValue(CBProperty, value);
            }
        }
        private static void OnCBChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var source = sender as UILMFKART;
            if (source != null)
            {
                source.CB = Helpers.Helper.Right("000" + (string)e.NewValue, 3);
                source.TMV.LMFRGBWERT2 = source.CB;
                source.CRGB = source.CR + source.CG+ source.CB;
            }
        }
        #endregion
        public VLMFKART TMV
        {
            get
            {
                return (VLMFKART)MV;
            }
        }
        public UILMFKART()
        {
           
        }
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            KEY = TMV.LMFKARTNR + TMV.LMFFARBE;
            FARBE = TMV.LMFFARBE;
            CR = TMV.LMFRGBWERT1;
            CG = TMV.LMFRGBWERT2;
            CB = TMV.LMFRGBWERT3;
        }
        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            base.Window_Loaded(sender, e);
        }
        public override void LOADALL()
        {
            OnEXAUFChg(EXAUF);
            base.LOADALL();
        } 
        public virtual void OnCARDNoChg(string value)
        {
            TBMain.Clear();
            if (value == "")
                return;
            TMV.LMLOAD(value);
        }
        public override void LOADBYAUF()
        {
            if (!IsCanLoad)
                return;
            TBMain.Clear();
            if (ORDERNUM.Length == 0)
                return;
            dwData = TBMain.NewRow();
            TMV.SetUpNewRow(ref dwData);
            dwData["LMFMANDANT"] = app.Mandant;
            dwData["LMFKARTNR"] = ORDERNUM.Substring(0,10);
            dwData["LMFFARBE"] = ORDERNUM.Substring(10, 10);
            SResult =TMV.QLMFARBE(dwData);
            MergeData(SResult, true);
            if (!IsAuto)
                return;
            if (TBMain.Rows.Count > 0)
                return;
            dwData = TBMain.NewRow();
            TMV.SetUpNewRow(ref dwData);
            dwData["LMFMANDANT"] = app.Mandant;
            dwData["LMFKARTNR"] = ORDERNUM.Substring(0, 10);
            dwData["LMFFARBE"] = ORDERNUM.Substring(10, 10);
            TMV.InsertLMFKART(dwData);
            TBMain.Rows.InsertAt(dwData, 0);
            vwMaster.View.MoveCurrentToFirst();
            base.LOADBYAUF();
        }
        public override void OnSave()
        {
            if (!IsSaved)
                return;
            foreach(DataRow row in TBMain.Rows)
            {
                if (row.RowState == DataRowState.Unchanged) continue;
                TMV.UpdateLMFKART(row, 0);
            }
            base.OnSave();
        }
    }
}

